/*
 * @Description:用户信息存储
 * @Autor: lcz
 * @LastEditors: lcz
 * @LastEditTime: 2023-10-09 09:21:45
 */
import * as types from "../../action-types";

import { CookieClass } from "@/utils/tools/auth";
const userInfoCookie = CookieClass.get(import.meta.env.VITE_APP_PROJECT_NAME_PREFIX + "_userInfo");
const userInfo = userInfoCookie ? JSON.parse(userInfoCookie) : {};
type userInfoActionType = Global.TranslateType<{ set_userInfo: any; clear_userInfo: any }>;

export function userInfoReducer(state = userInfo, action: userInfoActionType) {
  switch (action.type) {
    case types.SET_USERINFO:
      return state;
    case types.CLEAR_USERINFO:
      return {};
    default:
      return state;
  }
}

const token = CookieClass.get(import.meta.env.VITE_APP_PROJECT_NAME_PREFIX + "_token") || "";
type tokenActionType = Global.TranslateType<{
  set_token: {
    token: string;
  };
}>;
export function tokenReducer(state = token, action: tokenActionType) {
  switch (action.type) {
    case types.SET_TOKEN:
      return action.token;
    default:
      return state;
  }
}
